<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :inline="true" :model="queryParams" class="demo-form-inline">
      <el-form-item label="企业名称">
        <el-input v-model="queryParams.polluteName" placeholder="请输入企业名称"></el-input>
      </el-form-item>

      <el-form-item label="工艺名称">
        <el-input v-model="queryParams.name" placeholder="请输入工艺名称"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 新增按钮 -->
    <el-button type="primary" size="small" @click="handleAdd" style="margin-bottom: 15px;">+ 新增</el-button>

    <!-- 表格 -->
    <el-table :data="tableData" border style="width: 100%" v-loading="loading">
      <el-table-column type="index" label="序号" width="60" align="center"></el-table-column>
      <el-table-column prop="polluteName" label="企业名称" width="200"></el-table-column>
      <el-table-column prop="name" label="工艺名称" width="180"></el-table-column>
      <el-table-column prop="content" label="工艺内容"></el-table-column>
      <el-table-column prop="link" label="主要环节" width="180"></el-table-column>
      <el-table-column prop="equipment" label="所需设备" width="180"></el-table-column>
      <el-table-column label="操作" width="120">
        <template #default="{ row }">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(row)" >修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-top: 20px;"
    ></el-pagination>
    <!-- 新增对话框 -->
    <el-dialog :title="form.id?'编辑处理工艺':'添加处理工艺'" :visible.sync="dialogVisible" width="600px">
      <el-form :model="form" label-width="100px" :rules="rules" ref="form">
        <el-form-item label="企业名称" prop="polluteid">
          <el-select v-model="form.polluteid" placeholder="请选择企业">
            <el-option v-for="item in enterpriseList" :label="item.pollutename" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="工艺名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入工艺名称"></el-input>
        </el-form-item>

        <el-form-item label="工艺内容" prop="content">
          <el-input type="textarea" v-model="form.content" placeholder="请输入工艺内容"></el-input>
        </el-form-item>

        <el-form-item label="主要环节" prop="link">
          <el-input v-model="form.link" placeholder="请输入主要环节"></el-input>
        </el-form-item>

        <el-form-item label="所需设备" prop="equipment">
          <el-input v-model="form.equipment" placeholder="请输入所需设备"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {getEnterpriseList, getTechnologyList ,addTechnology,editTechnology,deleteTechnology} from './api.js'
export default {
  data() {
    return {
      queryParams: {
        polluteName: '',
        name: ''
      },
      currentPage: 1,
      pageSize: 10,
      total: 0, // 总数据条数
      tableData: [],
      enterpriseList:[],
      loading: false,
      dialogVisible: false, // 控制弹窗显示
      form: {
        polluteid: '',
        name: '',
        content: '',
        link: '',
        equipment: '',
        image: '' // 图片字段
      },
      rules: {
        polluteid: [{ required: true, message: '请选择企业', trigger: 'change' }],
        name: [{ required: true, message: '请输入工艺名称', trigger: 'blur' }],
        content: [{ required: true, message: '请输入工艺内容', trigger: 'blur' }],
        link: [{ required: true, message: '请输入主要环节', trigger: 'blur' }],
        equipment: [{ required: true, message: '请输入所需设备', trigger: 'blur' }],
        image: [{ required: false, message: '请上传图片', trigger: 'change' }]
      }
    };
  },
  created(){
    this.getEnterpriseList();
    this.getList()
  },
  methods: {

    // 获取企业列表
    async getEnterpriseList(){
      let res = await getEnterpriseList()
      if(res.code == 200) {
        this.enterpriseList = res.rows
      }
    },
    async getList(){
      let params = {
        ...this.queryParams,
        page: this.currentPage,
        pageSize: this.pageSize
      }
      let res = await getTechnologyList(params)
      if(res.code == 200) {
        this.tableData = res.rows
        this.total = res.total

      }
    },
    handleSearch() {
      this.getList();
      // 这里可以添加实际的搜索逻辑
    },
    resetForm() {
      this.queryParams.polluteName = '';
      this.queryParams.name = '';
    },
    handleAdd() {
      this.dialogVisible = true;
      this.form = {
        polluteName: '',
        name: '',
        content: '',
        link: '',
        equipment: '',
      };
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let apiFN = this.form.id ? editTechnology : addTechnology;
          apiFN(this.form).then(res =>{
            this.$message.success('提交成功');
            this.dialogVisible = false;
            this.getList(); // 刷新列表
          })
        } else {
          console.log('校验失败');
          return false;
        }
      });
    },
    handleEdit(row) {
      this.form = {...row};
      this.dialogVisible = true;
    },
    handleDelete(id) {
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTechnology(id).then(res =>{
          this.$message.success('删除成功');
          this.getList();
        })
        // 这里可以添加删除逻辑
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      // 这里可以添加分页大小改变的逻辑
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      // 这里可以添加当前页改变的逻辑
    }
  }
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>